"""
物流相关的数据验证模式
"""
from marshmallow import Schema, fields, validate, validates, ValidationError, post_load
from app.models.logistics import LogisticsStatus

class LogisticsProviderSchema(Schema):
    """物流服务商数据验证模式"""
    id = fields.Integer(dump_only=True)
    name = fields.String(required=True, validate=validate.Length(min=1, max=100))
    description = fields.String(allow_none=True)
    website = fields.String(allow_none=True)
    contact = fields.String(allow_none=True)
    is_active = fields.Boolean()
    created_at = fields.DateTime(dump_only=True)

class TrackingNodeSchema(Schema):
    """跟踪节点数据验证模式"""
    id = fields.Integer(dump_only=True)
    logistics_id = fields.Integer(dump_only=True)
    status = fields.String(required=True, validate=validate.OneOf([status.value for status in LogisticsStatus]))
    location = fields.String(allow_none=True)
    description = fields.String(allow_none=True)
    timestamp = fields.DateTime(dump_only=True)

class LogisticsSchema(Schema):
    """物流单数据验证模式"""
    id = fields.Integer(dump_only=True)
    order_id = fields.Integer(required=True)
    provider_id = fields.Integer(required=True)
    tracking_number = fields.String(required=True, validate=validate.Length(min=1, max=100))
    status = fields.String(dump_only=True)
    shipping_address = fields.String(required=True)
    shipping_date = fields.DateTime(allow_none=True)
    delivery_date = fields.DateTime(dump_only=True)
    created_at = fields.DateTime(dump_only=True)
    tracking_nodes = fields.List(fields.Nested(TrackingNodeSchema), dump_only=True)
    
    @validates('order_id')
    def validate_order_id(self, value):
        """验证订单ID"""
        from app.models.order import Order
        order = Order.query.get(value)
        if not order:
            raise ValidationError('订单不存在')
            
        # 检查订单是否已有物流单
        from app.models.logistics import Logistics
        logistics = Logistics.query.filter_by(order_id=value).first()
        if logistics and logistics.id != self.context.get('logistics_id'):
            raise ValidationError('该订单已有物流单')
    
    @validates('provider_id')
    def validate_provider_id(self, value):
        """验证物流商ID"""
        from app.models.logistics import LogisticsProvider
        provider = LogisticsProvider.query.get(value)
        if not provider:
            raise ValidationError('物流服务商不存在')
        if not provider.is_active:
            raise ValidationError('物流服务商未激活')
    
    @validates('tracking_number')
    def validate_tracking_number(self, value):
        """验证跟踪编号"""
        from app.models.logistics import Logistics
        logistics = Logistics.query.filter_by(tracking_number=value).first()
        if logistics and logistics.id != self.context.get('logistics_id'):
            raise ValidationError('跟踪编号已存在')

class LogisticsUpdateSchema(Schema):
    """物流单更新数据验证模式"""
    status = fields.String(validate=validate.OneOf([status.value for status in LogisticsStatus]))
    shipping_date = fields.DateTime(allow_none=True)
    delivery_date = fields.DateTime(allow_none=True)

class TrackingNodeCreateSchema(Schema):
    """跟踪节点创建数据验证模式"""
    logistics_id = fields.Integer(required=True)
    status = fields.String(required=True, validate=validate.OneOf([status.value for status in LogisticsStatus]))
    location = fields.String(allow_none=True)
    description = fields.String(allow_none=True)
    
    @validates('logistics_id')
    def validate_logistics_id(self, value):
        """验证物流单ID"""
        from app.models.logistics import Logistics
        logistics = Logistics.query.get(value)
        if not logistics:
            raise ValidationError('物流单不存在') 